package com.example.personal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.personal.entity.HttpStatusEnum;
import com.example.personal.entity.po.UserPO;
import com.example.personal.entity.vo.CommonResult;
import com.example.personal.entity.vo.UserVO;
import com.example.personal.repo.UserDao;
import com.example.personal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * Created on 2022/3/9.
 *
 * @author taodq
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserPO> implements UserService {

    @Autowired
    private UserDao mUserDao;

    @Override
    public CommonResult updateUserById(UserPO user) {
        return updateById(user) ? CommonResult.ok() : CommonResult.error();
    }

    @Override
    public CommonResult insert(UserPO user) {
//        if (user.getUserId() == null) {
//            user.setUserId(System.currentTimeMillis());
//        } else {
//            UserPO userPO = lambdaQuery().eq(UserPO::getUserId, user.getUserId()).one();
//            if (userPO != null) {
//                return CommonResult.error(HttpStatusEnum.USER_ALREADY_EXISTS_ERROR.getMsg());
//            }
//        }
        save(user);
        return CommonResult.ok();
    }

    @Override
    public CommonResult selectPage(Long current, Long pageSize) {
        if (current == null || ObjectUtils.isEmpty(current) || current <= 0) {
            return CommonResult.error(HttpStatusEnum.PARAM_CURRENT_PAGE_ERROR.getMsg());
        }

        if (pageSize == null || ObjectUtils.isEmpty(pageSize) || pageSize <= 0) {
            return CommonResult.error(HttpStatusEnum.PARAM_SIZE_PAGE_ERROR.getMsg());
        }

        UserVO userVO = new UserVO();
        IPage<UserPO> iPage = new Page<>(current, pageSize);
        mUserDao.selectPage(iPage, null);
        userVO.setCurrent(current);
        userVO.setSize(pageSize);
        userVO.setTotal(iPage.getTotal());
        userVO.setUserList(iPage.getRecords());
        return CommonResult.ok(userVO);
    }


}
